#!/usr/bin/python3
# coding: utf-8
# monitor_views.py
# 2018-09-13
# 监控视图
from flask import jsonify, abort, request, Blueprint
from app.ext import auth, db
from app.models import Items
import json


bp = Blueprint('monitor', __name__, url_prefix='/monitor')

@bp.route('/', methods=['GET'])
def index():
	return "<h1>Monitor's Index page<h1>"

@bp.route('/<int:item_id>', methods=['GET'])
def get_item(item_id):
	return jsonify({'task': [i.dict_item for i in Items.query.filter_by(id=item_id).all()]})

@bp.route('/items', methods=['GET'])
def get_items():
	return jsonify({'item': [i.dict_item for i in Items.query.order_by(Items.id.desc()).limit(20).all()]})

@bp.route('/add', methods=['POST'])
@auth.login_required
def add_items():
	datas = json.loads(request.data)
	for data in datas:
		item = Items(title=data['title'])
		db.session.add(item)
	db.session.commit()
	return jsonify({'task': datas}), 201
